package cn.nexd.map.coreLocator;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 15/8/30.
 */
public class coreFileLoader {
    public Double[][] opt_params;
    public String[] wifi_list;
    public Double[][] wp;

    /*
    Loading Way Point From Files
     */
    public void loadWp(String file_path) throws IOException {
        List<String> res = _loadOptParamsLines(file_path);
        this.wp = new Double[res.size()][2];
        for (int i = 0; i < res.size(); i++) {
            String[] ls = res.get(i).split(",");
            this.wp[i][0] = Double.parseDouble(ls[0]);
            this.wp[i][1] = Double.parseDouble(ls[1]);
        }
        res.clear();
    }

    public void init() {
        opt_params = null;
        wifi_list = null;
        wp = null;
    }

    public void loadWp(InputStream filecontent) throws IOException {
        List<String> res = _loadOptParamsLines(filecontent);
        this.wp = new Double[res.size()][2];
        for (int i = 0; i < res.size(); i++) {
            String[] ls = res.get(i).split(",");
            this.wp[i][0] = Double.parseDouble(ls[0]);
            this.wp[i][1] = Double.parseDouble(ls[1]);
        }
        res.clear();
    }




    /*
    Loading wifi_list From Files
     */

    public void loadWifiList(String file_path) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file_path));
        try {
            List<String> res = new ArrayList<>();
            String line = br.readLine();
            this.wifi_list = line.split(",");
        } finally {
            br.close();
        }
    }

    public void loadWifiList(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        try {
            List<String> res = new ArrayList<>();
            String line = bufferedReader.readLine();
            this.wifi_list = line.split(",");
        } finally {
            bufferedReader.close();
        }
    }


    /*
    Loading Optimized Data From Files
     */
    public void loadOptParams(String file_path) throws IOException {
        List<String> sb = _loadOptParamsLines(file_path);
        _initParamsArray(sb);
        _loadOptParams(sb);
    }

    public void loadOptParams(InputStream inputStream) throws IOException {
        List<String> sb = _loadOptParamsLines(inputStream);
        _initParamsArray(sb);
        _loadOptParams(sb);
    }

    private void _loadOptParams(List<String> sb) {
        for (int n = 0; n < sb.size(); n++) {
            String[] ls = sb.get(n).split(",");
            for (int i = 0; i < ls.length; i++) {
                this.opt_params[n][i] = Double.parseDouble(ls[i]);
            }
        }
        sb.clear();
    }

    private void _initParamsArray(List<String> sb) {
        int wifi_num = sb.size();
        String trial = sb.get(0);
        String[] ls = trial.split(",");
        int param_num = ls.length;
        this.opt_params = new Double[wifi_num][param_num];
    }

    private List<String> _loadOptParamsLines(String file_path) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file_path));
        try {
            List<String> res = new ArrayList<>();
            String line = br.readLine();

            while (line != null) {
                res.add(line);
                line = br.readLine();
            }
            return res;
        } finally {
            br.close();
        }
    }

    private List<String> _loadOptParamsLines(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        try {
            List<String> res = new ArrayList<>();
            String line = bufferedReader.readLine();

            while (line != null) {
                res.add(line);
                line = bufferedReader.readLine();
            }
            return res;
        } finally {
            bufferedReader.close();
        }
    }

}
